/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     日历和时间显示
 @FileName :    widget.cpp
 @Version :     1.0
****************************************************/


#include "widget.h"
#include "ui_widget.h"
#include <QTime>
#include <QComboBox>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QSpacerItem>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , is24HourFormat(true)
{
    ui->setupUi(this);
    
    ui->exit->raise();
    PushButton_calendar(ui->exit);
    
    ui->label_bj->setPixmap(QPixmap(":/主题背景"));
        
    ui->label_zt->setText("日 历");
    ui->label_zt->setStyleSheet("QLabel {"
        "    font-size: 50px;"           // 字体大小
        "    color: #d0dba8;"            // 文字颜色
        "    font-weight: bold;"         // 字体粗细
        "    font-family: '楷体';"        // 字体家族
        "}"
    );
    
    // 设置窗口大小为854x480
    resize(854, 480);
    
    // 设置日历样式
    ui->calendarWidget->setStyleSheet(R"(
        QCalendarWidget {
            background-color: white;
            border: none;
            border-radius: 15px;
            padding: 10px;
        }
        
        QCalendarWidget QToolButton {
            height: 30px;
            color: #2c3e50;
            font-weight: bold;
            background-color: #f8f9fa;
            border-radius: 8px;
            margin: 5px;
            padding: 5px;
        }
        
        QCalendarWidget QToolButton:hover {
            background-color: #e9ecef;
        }
        
        QCalendarWidget QMenu {
            background-color: white;
            border-radius: 8px;
            padding: 5px;
            selection-background-color: #3498db;
            selection-color: white;
        }
        
        QCalendarWidget QSpinBox {
            padding: 5px;
            border: 1px solid #e9ecef;
            border-radius: 8px;
            background-color: white;
            min-width: 60px;
        }
        
        QCalendarWidget QWidget#qt_calendar_navigationbar {
            background-color: #f8f9fa;
            border-radius: 10px;
            margin-bottom: 5px;
        }
        
        QCalendarWidget QAbstractItemView:enabled {
            color: #2c3e50;
            selection-background-color: #3498db;
            selection-color: white;
            selection-border-radius: 5px;
            gridline-color: #e9ecef;
            border-radius: 10px;
        }
        
        QCalendarWidget QAbstractItemView:enabled:alternate {
            background: #f8f9fa;
        }
        
        QCalendarWidget QAbstractItemView:disabled {
            color: #bdc3c7;
        }
    )");
    
    // 创建主垂直布局
    QVBoxLayout *mainVerticalLayout = new QVBoxLayout(this);
    mainVerticalLayout->setContentsMargins(20, 60, 20, 20); // 上60，左右下各20
    
    // 创建内容水平布局 - 使用所有可用空间
    QHBoxLayout *contentLayout = new QHBoxLayout();
    contentLayout->setSpacing(20);
    
    // 创建左侧时间面板
    QWidget *leftPanel = new QWidget(this);
    QVBoxLayout *leftLayout = new QVBoxLayout(leftPanel);
    leftLayout->setSpacing(15);
    
    // 创建时间显示标签
    QLabel *timeLabel = new QLabel(this);
    timeLabel->setObjectName("timeLabel");
    timeLabel->setAlignment(Qt::AlignCenter);
    timeLabel->setMinimumHeight(100);
    
    // 创建日期显示标签
    QLabel *dateLabel = new QLabel(this);
    dateLabel->setObjectName("dateLabel");
    dateLabel->setAlignment(Qt::AlignCenter);
    dateLabel->setMinimumHeight(60);
    
    // 创建控制面板
    QGroupBox *controlGroup = new QGroupBox("时间显示选项", this);

    // 设置样式表 字体颜色为白色，字体为楷体
    controlGroup->setStyleSheet(
        "QGroupBox {"
        "   font-size: 20px;"
        "   color: #ffffff;"           // 字体颜色
        "   font-family: 楷体;" // 字体家族，提供备选
        "}"
    );
    QVBoxLayout *controlLayout = new QVBoxLayout(controlGroup);
    
    // 创建样式选择下拉框
    QComboBox *styleCombo = new QComboBox(this);
    
    styleCombo->setMinimumHeight(30);  // 设置最小高度为30像素
    
    styleCombo->addItems({"默认样式", "深色模式", "蓝色科技", "简约白色", "黑色经典"});
    connect(styleCombo, &QComboBox::currentTextChanged, this, &Widget::changeStyle);
    
    // 创建24小时制切换按钮
    QPushButton *toggleFormatBtn = new QPushButton("切换12/24小时制", this);
    connect(toggleFormatBtn, &QPushButton::clicked, this, &Widget::toggleTimeFormat);
    
    toggleFormatBtn->setFixedHeight(30);
    
    QLabel *label = new QLabel("显示样式:", this);
    label->setFixedHeight(30);
    label->setStyleSheet("QLabel {"
                         "color: white;"
                         "font-family: 楷体;"
                         "font-size: 18px;"
                         "}");
    controlLayout->addWidget(label);
    controlLayout->addWidget(styleCombo);
    controlLayout->addSpacing(10);
    controlLayout->addWidget(toggleFormatBtn);
    
    // 添加到左侧布局
    leftLayout->addWidget(timeLabel);
    leftLayout->addWidget(dateLabel);
    leftLayout->addWidget(controlGroup);
    leftLayout->setStretch(0, 3); // 时间标签
    leftLayout->setStretch(1, 2); // 日期标签
    leftLayout->setStretch(2, 2); // 控制组
    
    // 右侧直接使用calendarWidget
    QWidget *rightPanel = new QWidget(this);
    QVBoxLayout *rightLayout = new QVBoxLayout(rightPanel);
    rightLayout->setContentsMargins(0, 0, 0, 0);
    rightLayout->addWidget(ui->calendarWidget);
    
    // 添加到内容水平布局
    contentLayout->addWidget(leftPanel, 1); // 左侧占1份
    contentLayout->addWidget(rightPanel, 2); // 右侧占2份
    
    // 将内容布局添加到主垂直布局
    mainVerticalLayout->addLayout(contentLayout, 1); // 内容区域占满剩余空间
    
    // 设置主布局
    setLayout(mainVerticalLayout);
    
    // 设置初始样式为简约白色
    styleCombo->setCurrentText("简约白色");
    changeStyle("简约白色");
    
    // 创建定时器，每秒更新一次时间
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Widget::updateTime);
    timer->start(1000);
    
    // 立即更新一次时间
    updateTime();
}

void Widget::updateTime() {
    QTime currentTime = QTime::currentTime();
    QDate currentDate = QDate::currentDate();
    
    QLabel *timeLabel = findChild<QLabel*>("timeLabel");
    QLabel *dateLabel = findChild<QLabel*>("dateLabel");
    
    if (timeLabel && dateLabel) {
        if (is24HourFormat) {
            timeLabel->setText(currentTime.toString("HH:mm:ss"));
        } else {
            timeLabel->setText(currentTime.toString("h:mm:ss AP"));
        }
        dateLabel->setText(currentDate.toString("yyyy年MM月dd日 dddd"));
    }
}

void Widget::changeStyle(const QString &style) {
    QLabel *timeLabel = findChild<QLabel*>("timeLabel");
    QLabel *dateLabel = findChild<QLabel*>("dateLabel");
    
    if (!timeLabel || !dateLabel) return;
    
    if (style == "默认样式") {
        timeLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #2c3e50;"
            "   color: #ecf0f1;"
            "   border-radius: 15px;"
            "   padding: 15px;"
            "   font-size: 32px;"
            "   font-weight: bold;"
            "}"
        );
        dateLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #2c3e50;"
            "   color: #3498db;"
            "   border-radius: 10px;"
            "   padding: 10px;"
            "   font-size: 16px;"
            "   font-weight: bold;"
            "}"
        );
    } else if (style == "深色模式") {
        timeLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #1a1a1a;"
            "   color: #e6e6e6;"
            "   border-radius: 15px;"
            "   padding: 15px;"
            "   font-size: 32px;"
            "   font-weight: bold;"
            "}"
        );
        dateLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #1a1a1a;"
            "   color: #7f8c8d;"
            "   border-radius: 10px;"
            "   padding: 10px;"
            "   font-size: 16px;"
            "   font-weight: bold;"
            "}"
        );
    } else if (style == "蓝色科技") {
        timeLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #0c1a2d;"
            "   color: #00b4ff;"
            "   border-radius: 15px;"
            "   padding: 15px;"
            "   font-size: 32px;"
            "   font-weight: bold;"
            "}"
        );
        dateLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #0c1a2d;"
            "   color: #0066cc;"
            "   border-radius: 10px;"
            "   padding: 10px;"
            "   font-size: 16px;"
            "   font-weight: bold;"
            "}"
        );
    } else if (style == "简约白色") {
        timeLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #ffffff;"
            "   color: #2c3e50;"
            "   border-radius: 15px;"
            "   padding: 15px;"
            "   font-size: 32px;"
            "   font-weight: bold;"
            "   border: 2px solid #bdc3c7;"
            "}"
        );
        dateLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #ffffff;"
            "   color: #2c3e50;"
            "   border-radius: 10px;"
            "   padding: 10px;"
            "   font-size: 16px;"
            "   font-weight: bold;"
            "   border: 1px solid #bdc3c7;"
            "}"
        );
    } else if (style == "黑色经典") {
        timeLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #000000;"
            "   color: #f1c40f;"
            "   border-radius: 15px;"
            "   padding: 15px;"
            "   font-size: 32px;"
            "   font-weight: bold;"
            "}"
        );
        dateLabel->setStyleSheet(
            "QLabel {"
            "   background-color: #000000;"
            "   color: #f1c40f;"
            "   border-radius: 10px;"
            "   padding: 10px;"
            "   font-size: 16px;"
            "   font-weight: bold;"
            "}"
        );
    }
}

void Widget::toggleTimeFormat() {
    is24HourFormat = !is24HourFormat;
    updateTime();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_exit_clicked()
{
    this->close();
    QApplication::quit();
}

void Widget::PushButton_calendar(QPushButton* PushButton)
{
    PushButton->setStyleSheet(R"(
        QPushButton {
            background-color: #d0dba8;
            color: white;
            border-radius: 15px;
            border: none;
            padding: 4px 4px;
            font-size: 20px;
            font-weight: bold;
            font-family: "楷体";
        }
        QPushButton:hover {
            background-color: #3367d6;
        }
        QPushButton:pressed {
            background-color: #2a56c6;
        }
    )");
}

